<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - coverage.lcov - src/mqtt_client_publishing_manager.dart</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - mqtt_client_publishing_manager.dart<span style="font-size: 80%;"> (source / <a href="mqtt_client_publishing_manager.dart.func.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">coverage.lcov</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">58</td>
            <td class="headerCovTableEntry">58</td>
            <td class="headerCovTableEntryHi">100.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2017-10-09</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntryHi">-</td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /*</a>
<span class="lineNum">       2 </span>            :  * Package : mqtt_client
<span class="lineNum">       3 </span>            :  * Author : S. Hamblett &lt;steve.hamblett@linux.com&gt;
<span class="lineNum">       4 </span>            :  * Date   : 30/06/2017
<span class="lineNum">       5 </span>            :  * Copyright :  S.Hamblett
<span class="lineNum">       6 </span>            :  */
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : part of mqtt_client;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : /// Handles the logic and workflow surrounding the message publishing and receipt process.
<span class="lineNum">      11 </span>            : ///
<span class="lineNum">      12 </span>            : ///         It's probably worth going into a bit of the detail around publishing and Quality of Service levels
<span class="lineNum">      13 </span>            : ///         as they are primarily the reason why message publishing has been split out into this class.
<span class="lineNum">      14 </span>            : ///
<span class="lineNum">      15 </span>            : ///         There are 3 different QOS levels. AtMostOnce (0), means that the message, when sent from broker to client, or
<span class="lineNum">      16 </span>            : ///         client to broker, should be delivered at most one time, and it does not matter if the message is
<span class="lineNum">      17 </span>            : ///         &quot;lost&quot;. QOS 2, AtLeastOnce, means that the message should be successfully received by the receiving
<span class="lineNum">      18 </span>            : ///         party at least one time, so requires some sort of acknowledgement so the sender can re-send if the
<span class="lineNum">      19 </span>            : ///         receiver does not acknowledge.
<span class="lineNum">      20 </span>            : ///
<span class="lineNum">      21 </span>            : ///         QOS 3 is a bit more complicated as it provides the facility for guaranteed delivery of the message
<span class="lineNum">      22 </span>            : ///         exactly one time, no more, no less.
<span class="lineNum">      23 </span>            : ///
<span class="lineNum">      24 </span>            : ///         Each of these have different message flow between the sender and receiver.&lt;/para&gt;
<span class="lineNum">      25 </span>            : ///         QOS 0 - AtMostOnce
<span class="lineNum">      26 </span>            : ///           Sender --&gt; Publish --&gt; Receiver
<span class="lineNum">      27 </span>            : ///         QOS 1 - AtLeastOnce
<span class="lineNum">      28 </span>            : ///           Sender --&gt; Publish --&gt; Receiver --&gt; PublishAck --&gt; Sender
<span class="lineNum">      29 </span>            : ///                                      |
<span class="lineNum">      30 </span>            : ///                                      v
<span class="lineNum">      31 </span>            : ///                               Message Processor
<span class="lineNum">      32 </span>            : ///         QOS 2 - AtLeastOnce
<span class="lineNum">      33 </span>            : ///         Sender --&gt; Publish --&gt; Receiver --&gt; PublishReceived --&gt; Sender --&gt; PublishRelease --&gt; Reciever --&gt; PublishComplete --&gt; Sender
<span class="lineNum">      34 </span>            : ///                                                                                                   | v
<span class="lineNum">      35 </span>            : ///                                                                                            Message Processor
<span class="lineNum">      36 </span>            : class PublishingManager extends events.EventEmitter
<span class="lineNum">      37 </span>            :     implements IPublishingManager {
<span class="lineNum">      38 </span>            :   /// Handles dispensing of message ids for messages published to a topic.
<span class="lineNum">      39 </span>            :   MessageIdentifierDispenser messageIdentifierDispenser =
<span class="lineNum">      40 </span>            :   new MessageIdentifierDispenser();
<span class="lineNum">      41 </span>            : 
<span class="lineNum">      42 </span>            :   /// Stores messages that have been pubished but not yet acknowledged.
<span class="lineNum">      43 </span>            :   Map&lt;int, MqttPublishMessage&gt; publishedMessages =
<span class="lineNum">      44 </span>            :   new Map&lt;int, MqttPublishMessage&gt;();
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            :   /// Stores messages that have been received from a broker with qos level 2 (Exactly Once).
<span class="lineNum">      47 </span>            :   Map&lt;int, MqttPublishMessage&gt; receivedMessages =
<span class="lineNum">      48 </span>            :   new Map&lt;int, MqttPublishMessage&gt;();
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            :   /// Stores a cache of data converters used when publishing data to a broker.
<span class="lineNum">      51 </span>            :   Map&lt;Type, Object&gt; dataConverters = new Map&lt;Type, Object&gt;();
<span class="lineNum">      52 </span>            : 
<span class="lineNum">      53 </span>            :   /// The current connection handler.
<span class="lineNum">      54 </span>            :   IMqttConnectionHandler connectionHandler;
<span class="lineNum">      55 </span>            : 
<span class="lineNum">      56 </span>            :   /// Raised when a message has been recieved by the client and the relevant QOS handshake is complete.
<span class="lineNum">      57 </span>            :   MessageReceived publishEvent;
<span class="lineNum">      58 </span>            : 
<span class="lineNum">      59 </span>            :   /// Initializes a new instance of the PublishingManager class.
<span class="lineNum">      60 </span><span class="lineCov">          3 :   PublishingManager(IMqttConnectionHandler connectionHandler) {</span>
<span class="lineNum">      61 </span><span class="lineCov">          3 :     this.connectionHandler = connectionHandler;</span>
<span class="lineNum">      62 </span><span class="lineCov">          3 :     connectionHandler.registerForMessage(</span>
<span class="lineNum">      63 </span><span class="lineCov">          3 :         MqttMessageType.publishAck, handlePublishAcknowledgement);</span>
<span class="lineNum">      64 </span><span class="lineCov">          3 :     connectionHandler.registerForMessage(</span>
<span class="lineNum">      65 </span><span class="lineCov">          3 :         MqttMessageType.publish, handlePublish);</span>
<span class="lineNum">      66 </span><span class="lineCov">          3 :     connectionHandler.registerForMessage(</span>
<span class="lineNum">      67 </span><span class="lineCov">          3 :         MqttMessageType.publishComplete, handlePublishComplete);</span>
<span class="lineNum">      68 </span><span class="lineCov">          3 :     connectionHandler.registerForMessage(</span>
<span class="lineNum">      69 </span><span class="lineCov">          3 :         MqttMessageType.publishRelease, handlePublishRelease);</span>
<span class="lineNum">      70 </span><span class="lineCov">          3 :     connectionHandler.registerForMessage(</span>
<span class="lineNum">      71 </span><span class="lineCov">          3 :         MqttMessageType.publishReceived, handlePublishReceived);</span>
<span class="lineNum">      72 </span>            :   }
<span class="lineNum">      73 </span>            : 
<span class="lineNum">      74 </span>            :   /// Publish a message to the broker on the specified topic.
<span class="lineNum">      75 </span>            :   /// The topic to send the message to
<span class="lineNum">      76 </span>            :   /// The QOS to use when publishing the message.
<span class="lineNum">      77 </span>            :   /// The message to send.
<span class="lineNum">      78 </span>            :   /// The message identifier assigned to the message.
<span class="lineNum">      79 </span>            :   int publish(PublicationTopic topic, MqttQos qualityOfService,
<span class="lineNum">      80 </span>            :       typed.Uint8Buffer data) {
<span class="lineNum">      81 </span><span class="lineCov">          2 :     final int msgId = messageIdentifierDispenser</span>
<span class="lineNum">      82 </span><span class="lineCov">          4 :         .getNextMessageIdentifier(&quot;topic:{$topic.toString()}&quot;);</span>
<span class="lineNum">      83 </span><span class="lineCov">          2 :     final MqttPublishMessage msg = new MqttPublishMessage()</span>
<span class="lineNum">      84 </span><span class="lineCov">          4 :         .toTopic(topic.toString())</span>
<span class="lineNum">      85 </span><span class="lineCov">          2 :         .withMessageIdentifier(msgId)</span>
<span class="lineNum">      86 </span><span class="lineCov">          2 :         .withQos(qualityOfService)</span>
<span class="lineNum">      87 </span><span class="lineCov">          2 :         .publishData(data);</span>
<span class="lineNum">      88 </span>            :     // QOS level 1 or 2 messages need to be saved so we can do the ack processes
<span class="lineNum">      89 </span><span class="lineCov">          2 :     if (qualityOfService == MqttQos.atLeastOnce ||</span>
<span class="lineNum">      90 </span><span class="lineCov">          2 :         qualityOfService == MqttQos.exactlyOnce) {</span>
<span class="lineNum">      91 </span><span class="lineCov">          4 :       publishedMessages[msgId] = msg;</span>
<span class="lineNum">      92 </span>            :     }
<span class="lineNum">      93 </span><span class="lineCov">          4 :     connectionHandler.sendMessage(msg);</span>
<span class="lineNum">      94 </span>            :     return msgId;
<span class="lineNum">      95 </span>            :   }
<span class="lineNum">      96 </span>            : 
<span class="lineNum">      97 </span>            :   /// Handles the receipt of publish acknowledgement messages.
<span class="lineNum">      98 </span>            :   /// This callback simply removes it from the list of published messages.
<span class="lineNum">      99 </span>            :   bool handlePublishAcknowledgement(MqttMessage msg) {
<span class="lineNum">     100 </span><span class="lineCov">          1 :     final MqttPublishAckMessage ackMsg = msg as MqttPublishAckMessage;</span>
<span class="lineNum">     101 </span>            :     // If we're expecting an ack for the message, remove it from the list of pubs awaiting ack.
<span class="lineNum">     102 </span><span class="lineCov">          2 :     if (publishedMessages.keys</span>
<span class="lineNum">     103 </span><span class="lineCov">          3 :         .contains(ackMsg.variableHeader.messageIdentifier)) {</span>
<span class="lineNum">     104 </span><span class="lineCov">          4 :       publishedMessages.remove(ackMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     105 </span>            :     }
<span class="lineNum">     106 </span>            :     return true;
<span class="lineNum">     107 </span>            :   }
<span class="lineNum">     108 </span>            : 
<span class="lineNum">     109 </span>            :   /// Handles the receipt of publish messages from a message broker.
<span class="lineNum">     110 </span>            :   bool handlePublish(MqttMessage msg) {
<span class="lineNum">     111 </span><span class="lineCov">          2 :     final MqttPublishMessage pubMsg = msg as MqttPublishMessage;</span>
<span class="lineNum">     112 </span>            :     bool publishSuccess = true;
<span class="lineNum">     113 </span>            :     try {
<span class="lineNum">     114 </span>            :       final PublicationTopic topic =
<span class="lineNum">     115 </span><span class="lineCov">          6 :       new PublicationTopic(pubMsg.variableHeader.topicName);</span>
<span class="lineNum">     116 </span><span class="lineCov">          6 :       if (pubMsg.header.qos == MqttQos.atMostOnce) {</span>
<span class="lineNum">     117 </span>            :         // QOS AtMostOnce 0 require no response.
<span class="lineNum">     118 </span>            :         // Send the message for processing to whoever is waiting.
<span class="lineNum">     119 </span><span class="lineCov">          4 :         emitEvent(new MessageReceived(topic, msg));</span>
<span class="lineNum">     120 </span><span class="lineCov">          6 :       } else if (pubMsg.header.qos == MqttQos.atLeastOnce) {</span>
<span class="lineNum">     121 </span>            :         // QOS AtLeastOnce 1 require an acknowledgement
<span class="lineNum">     122 </span>            :         // Send the message for processing to whoever is waiting.
<span class="lineNum">     123 </span><span class="lineCov">          4 :         emitEvent(new MessageReceived(topic, msg));</span>
<span class="lineNum">     124 </span><span class="lineCov">          2 :         final MqttPublishAckMessage ackMsg = new MqttPublishAckMessage()</span>
<span class="lineNum">     125 </span><span class="lineCov">          6 :             .withMessageIdentifier(pubMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     126 </span><span class="lineCov">          4 :         connectionHandler.sendMessage(ackMsg);</span>
<span class="lineNum">     127 </span><span class="lineCov">          3 :       } else if (pubMsg.header.qos == MqttQos.exactlyOnce) {</span>
<span class="lineNum">     128 </span>            :         // QOS ExactlyOnce means we can't give it away yet, we gotta do a handshake
<span class="lineNum">     129 </span>            :         // to make sure the broker knows we got it, and we know he knows we got it.
<span class="lineNum">     130 </span>            :         // If we've already got it thats ok, it just means its being republished because
<span class="lineNum">     131 </span>            :         // of a handshake breakdown, overwrite our existing one for the sake of it
<span class="lineNum">     132 </span><span class="lineCov">          1 :         if (!receivedMessages</span>
<span class="lineNum">     133 </span><span class="lineCov">          3 :             .containsKey(pubMsg.variableHeader.messageIdentifier)) {</span>
<span class="lineNum">     134 </span><span class="lineCov">          4 :           receivedMessages[pubMsg.variableHeader.messageIdentifier] = pubMsg;</span>
<span class="lineNum">     135 </span>            :         }
<span class="lineNum">     136 </span>            :         final MqttPublishReceivedMessage pubRecv =
<span class="lineNum">     137 </span><span class="lineCov">          1 :         new MqttPublishReceivedMessage()</span>
<span class="lineNum">     138 </span><span class="lineCov">          3 :             .withMessageIdentifier(pubMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     139 </span><span class="lineCov">          2 :         connectionHandler.sendMessage(pubRecv);</span>
<span class="lineNum">     140 </span>            :       }
<span class="lineNum">     141 </span>            :     } catch (Exception) {
<span class="lineNum">     142 </span>            :       publishSuccess = false;
<span class="lineNum">     143 </span>            :     }
<span class="lineNum">     144 </span>            :     return publishSuccess;
<span class="lineNum">     145 </span>            :   }
<span class="lineNum">     146 </span>            : 
<span class="lineNum">     147 </span>            :   /// Handles the publish complete, for messages that are undergoing Qos ExactlyOnce processing.
<span class="lineNum">     148 </span>            :   bool handlePublishRelease(MqttMessage msg) {
<span class="lineNum">     149 </span>            :     final MqttPublishReleaseMessage pubRelMsg =
<span class="lineNum">     150 </span><span class="lineCov">          1 :     msg as MqttPublishReleaseMessage;</span>
<span class="lineNum">     151 </span>            :     bool publishSuccess = true;
<span class="lineNum">     152 </span>            :     try {
<span class="lineNum">     153 </span>            :       final MqttPublishMessage pubMsg =
<span class="lineNum">     154 </span><span class="lineCov">          4 :       receivedMessages.remove(pubRelMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     155 </span>            :       if (pubMsg != null) {
<span class="lineNum">     156 </span>            :         // Send the message for processing to whoever is waiting.
<span class="lineNum">     157 </span>            :         final PublicationTopic topic =
<span class="lineNum">     158 </span><span class="lineCov">          3 :         new PublicationTopic(pubMsg.variableHeader.topicName);</span>
<span class="lineNum">     159 </span><span class="lineCov">          2 :         emitEvent(new MessageReceived(topic, pubMsg));</span>
<span class="lineNum">     160 </span>            :         final MqttPublishCompleteMessage compMsg =
<span class="lineNum">     161 </span><span class="lineCov">          1 :         new MqttPublishCompleteMessage()</span>
<span class="lineNum">     162 </span><span class="lineCov">          3 :             .withMessageIdentifier(pubMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     163 </span><span class="lineCov">          2 :         connectionHandler.sendMessage(compMsg);</span>
<span class="lineNum">     164 </span>            :       }
<span class="lineNum">     165 </span>            :     } catch (Exception) {
<span class="lineNum">     166 </span>            :       publishSuccess = false;
<span class="lineNum">     167 </span>            :     }
<span class="lineNum">     168 </span>            :     return publishSuccess;
<span class="lineNum">     169 </span>            :   }
<span class="lineNum">     170 </span>            : 
<span class="lineNum">     171 </span>            :   /// Handles a publish complete message received from a broker.
<span class="lineNum">     172 </span>            :   /// Returns true if the message flow completed successfully, otherwise false.
<span class="lineNum">     173 </span>            :   bool handlePublishComplete(MqttMessage msg) {
<span class="lineNum">     174 </span>            :     final MqttPublishCompleteMessage compMsg =
<span class="lineNum">     175 </span><span class="lineCov">          1 :     msg as MqttPublishCompleteMessage;</span>
<span class="lineNum">     176 </span>            :     final MqttPublishMessage ok =
<span class="lineNum">     177 </span><span class="lineCov">          4 :     publishedMessages.remove(compMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     178 </span>            :     if (ok != null) {
<span class="lineNum">     179 </span>            :       return true;
<span class="lineNum">     180 </span>            :     }
<span class="lineNum">     181 </span>            :     return false;
<span class="lineNum">     182 </span>            :   }
<span class="lineNum">     183 </span>            : 
<span class="lineNum">     184 </span>            :   /// Handles publish received messages during processing of QOS level 2 (Exactly once) messages.
<span class="lineNum">     185 </span>            :   /// Returns true or false, depending on the success of message processing.
<span class="lineNum">     186 </span>            :   bool handlePublishReceived(MqttMessage msg) {
<span class="lineNum">     187 </span>            :     final MqttPublishReceivedMessage recvMsg =
<span class="lineNum">     188 </span><span class="lineCov">          1 :     msg as MqttPublishReceivedMessage;</span>
<span class="lineNum">     189 </span>            :     // If we've got a matching message, respond with a &quot;ok release it for processing&quot;
<span class="lineNum">     190 </span><span class="lineCov">          1 :     if (publishedMessages</span>
<span class="lineNum">     191 </span><span class="lineCov">          3 :         .containsKey(recvMsg.variableHeader.messageIdentifier)) {</span>
<span class="lineNum">     192 </span><span class="lineCov">          1 :       final MqttPublishReleaseMessage relMsg = new MqttPublishReleaseMessage()</span>
<span class="lineNum">     193 </span><span class="lineCov">          3 :           .withMessageIdentifier(recvMsg.variableHeader.messageIdentifier);</span>
<span class="lineNum">     194 </span><span class="lineCov">          2 :       connectionHandler.sendMessage(relMsg);</span>
<span class="lineNum">     195 </span>            :     }
<span class="lineNum">     196 </span>            :     return true;
<span class="lineNum">     197 </span>            :   }
<span class="lineNum">     198 </span>            : }
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.10</a></td></tr>
  </table>
  <br>

</body>
</html>
